package org.itboys.product.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.itboys.commons.CommonConstants;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.resource.ResourceHolder;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mongodb.utils.page.Page;
import org.itboys.product.entity.ProductCategorySpecDetail;
import org.itboys.product.service.ProductCategorySpecDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
/**
 * 规格特征详情
 * @author weisky
 *
 */
@Controller
@RequestMapping("/product/categorySpecDetail")
public class ProductCategorySpecDetailController extends BaseController{

	@Autowired
	private ProductCategorySpecDetailService productCategorySpecDetailService;
	@Autowired
	private ResourceHolder resourceHolder;
	
	@RequestMapping("/toCategorySpecDetailVM")
	public String toCategorySpecDetailVM(@RequestParam Long productCategoryId,
			@RequestParam Long specId,
			Model model, HttpServletRequest request){
		model.addAttribute("productCategoryId", productCategoryId);
		model.addAttribute("specId", specId);
		return "/ace/product/categorySpecDetail";
	}
	
	@RequestMapping("/list")
	public void list(HttpServletRequest request,HttpServletResponse response){
		Page<ProductCategorySpecDetail> page = productCategorySpecDetailService.pageQuery(request);
		JsonPageUtils.renderJsonPage(page.getTotal(), page.getData(), response);
	}
	
	@RequestMapping("/save")
	public void insert(ProductCategorySpecDetail productCategorySpecDetail,
			@RequestParam(value="tImage", required=false) MultipartFile tImage,
			HttpServletRequest request,HttpServletResponse response){
		boolean result = productCategorySpecDetailService.insert(productCategorySpecDetail, tImage);
		if(result){
			AjaxUtils.renderText(response, CommonConstants.SUCCESS);
		}else{
			AjaxUtils.renderText(response, CommonConstants.FAIL);
		}
	}
	
	@RequestMapping("/getById")
	public void getById(@RequestParam("id") Long id,
			HttpServletRequest request, HttpServletResponse response){
		AjaxUtils.renderJson(response, productCategorySpecDetailService.getById(id));
	}
	
	@RequestMapping("/delete")
	public void delete(@RequestParam("id") Long id,
			HttpServletRequest request,HttpServletResponse response){
		boolean result = productCategorySpecDetailService.dele(id);
		if(result){
			AjaxUtils.renderText(response, CommonConstants.SUCCESS);
		}else{
			AjaxUtils.renderText(response, CommonConstants.FAIL);
		}
	}
}
